<template>
	<div class="container batch-report" v-title data-title='批量报备'>
		<!-- currentStep = 1  填写用户基础信息-->
		<fill-base-info
				ref="fillBaseInfo"
				:premise-list="selectedList"
				v-show="currentStep === STEP.ONE"
		/>
		<!-- currentStep = 2  选择意向楼盘 -->
		<intention-premises-list-for-many
				ref="intentionPremises"
				:list="selectedList"
				@choose-premise="choosePremise"
				v-show="currentStep === STEP.TWO"
		/>
		<!--currentStep = 3  填写带看时间 -->
		<fill-visiting-time
			:list="selectedList"
			@add-premise="addPremise"
			@delete-premise="deletePremise"
			@submit="submitReportPremise"
			ref="fillVisitingTime"
			v-show="currentStep === STEP.THREE"
	/>
		<div>
			<button
					v-if="currentStep === STEP.ONE"
					class="btn-block" @click="checkCustomerPremises()" >
				下一步，选择到访时间</button>
		</div>
	</div>
</template>
<script>
	import FillBaseInfo from "./components/FillBaseInfo.vue"
	import IntentionPremisesListForMany from "./components/IntentionPremisesListForMany.vue"
	import FillVisitingTime from "./components/FillVisitingTime.vue"
	import { Service, Utils} from '../../../js/base'
	const STEP = {
		ONE: 1, // 填写用户基础信息
		TWO: 2, // 选择意向楼盘
		THREE: 3 // 填写带看时间
	}
	export default{
		components: {
			FillBaseInfo,
			IntentionPremisesListForMany,
			FillVisitingTime
		},
		data(){
			return {
				STEP: STEP,
				currentStep: STEP.ONE, //
				selectedList: []
			}
		},
		mounted() {
			this.init()
		},
		methods: {
			init() {
				// layer.close(window.addCustomerIndex)
			},
			/**
			 * 下一步
			 */
			checkCustomerPremises () {
				if(!this.checkParams ()) {
					return
				}
				this.currentStep = this.STEP.THREE
			},
			/**
			 * 添加楼盘
			 */
			addPremise() {
				this.currentStep = this.STEP.TWO ;
			},
			/**
			 * 删除楼盘
			 */
			deletePremise (row) {//
				this.selectedList = this.selectedList.filter(item => row.premisesId !== item.premisesId)
			},
			/**
			 * 已选择的楼盘
			 */
			choosePremise (list) {
				this.selectedList = list
				this.currentStep = this.STEP.ONE ;// 显示楼盘选择
			},
			/**
			 * 提交报备
			 */
			submitReportPremise () {
				this.checkReport().then(rsp => {
					this.selectedList.forEach(item => {
						item.errorMsg = '' // 清空错误信息
					})
					this.saveReport ()
				}).catch(e => {
					// Utils.showWarnMsg("存在不符合报备条件的楼盘，请先处理完毕后再进行提交。")
				})
			},
			/**
			 * 校验数据
			 */
			checkReport() {
				let _this = this;
				if(!this.checkParams) {
					return
				}
				let params = this.getCheckParams ()
				return Service.Premise.checkReportList({params, _this})
						.then(rsp => {
							return rsp
						}).catch( err => {
							let errorMsg = err.resData && err.resData.resultMsg || "校验失败"
							if(err && err.result === '0') {
								let errorList = err.resData.batchReportInfo
								if(errorList) {
									let errorIndex= -1
									errorList.forEach( item => {
										errorIndex = _this.selectedList.findIndex( i => i.premisesId ===item.premisesId) // 找到错误的那条数据
										if(errorIndex > -1) {
											_this.$set(_this.selectedList[errorIndex], 'errorMsg', item.errorMsg)
											_this.$set(_this.selectedList[errorIndex], 'ifSelect', item.ifSelect)
											//_this.selectedList[errorIndex].errorMsg = item.errorMsg
										}
									})
									return Promise.reject(err)
								} else {
									Utils.showFailMsg(errorMsg)
									return Promise.reject(err)
								}
							} else {
								Utils.showFailMsg(errorMsg)
								return Promise.reject(err)
							}
						})
			},
			/**
			 * 保存报备
			 */
			saveReport () {
				let _this = this;
				let params = this.getCheckParams ()
				Service.Premise.saveReportList({params, _this})
						.then(rsp => {
							Utils.showSuccessMsg("报备成功");
							_this.$router.push({
								path: '/declare'
							})
						}).catch( err => {
					Utils.showFailMsg("报备失败")
				})
			},
			/**
			 * 整理报备校验、保存接口参数
			 */
			getCheckParams (saveFlag = false) {
				let baseInfo = this.$refs.fillBaseInfo.form
				let mobile = baseInfo.mobile
				if(!baseInfo.isShow) {// 隐号报备
					mobile = mobile.slice(0,3) + "****" + mobile.slice(7,12)
				}
				let params ={
					reportType: '1', // ???
					staffId: localStorage.getItem('3_userId'), // 。。。
					name: baseInfo.name,
					mobile: mobile,
					gender: baseInfo.gender,
					remark: baseInfo.remark,
					//...baseInfo,
					"reportInfoList": [],
				}
				let reportInfoItem = {}
				this.selectedList.forEach(item => {
					reportInfoItem = {
						"hopeVisitTime": item.hopeVisitTime ||  "", //&& moment(item.hopeVisitTime).format(DATE_FORMAT) ||
						"premisesId": item.premisesId,
						"recommender": item.recommender || '', // "bbcec88d98c440d9bf3ff12a0c68cc46", //
						"specialTime": item.specialTime, //楼盘最短报备时间
						"premisesName": item.premisesName,
					}
					if(saveFlag) {
						delete params.specialTime
						delete params.premisesName
					}
					params.reportInfoList.push(reportInfoItem)
				})
				delete params.progressShow
				delete params.premiseNum
				return params;
			},
			/**
			 * 检验参数
			 * @returns {boolean}
			 */
			checkParams () {
				let baseInfo = this.$refs.fillBaseInfo.form
				if(!baseInfo.name) {
					Utils.showWarnMsg("请输入姓名")
					return  false
				}
				if(!baseInfo.mobile) {
					Utils.showWarnMsg("请输入手机号")
					return  false
				}
				if(!Utils.checkPhone(baseInfo.mobile)) {
					Utils.showWarnMsg("手机号格式有误")
					return  false
				}
				if(baseInfo.remark && baseInfo.remark.length > 200) {
					Utils.showWarnMsg("备注不能超过200个字")
					return  false
				}
				return true
			},
		}
	}

</script>
<style lang="less"scoped>
	.batch-report {
		.btn-block{
			width: 90%;
			height: 40px;
			display: block;
			margin:36px auto;
			background-color: #ff7900;
			color: #fff;
			font-size: 14px;
			line-height: 40px;
			border-radius: 4px;
		}

	}
</style>
